package com.qingjiang.projectschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingjiang.projectschedule.mapper.ManageDeptMapper;
import com.qingjiang.projectschedule.mapper.UserInfoMapper;
import com.qingjiang.projectschedule.pojo.Department;
import com.qingjiang.projectschedule.pojo.ManageDept;
import com.qingjiang.projectschedule.pojo.Role;
import com.qingjiang.projectschedule.pojo.UserInfo;
import com.qingjiang.projectschedule.service.DepartmentService;
import com.qingjiang.projectschedule.service.RoleService;
import com.qingjiang.projectschedule.service.UserInfoService;
import com.qingjiang.projectschedule.service.UserService;
import com.qingjiang.projectschedule.utils.JwtUtil;
import com.qingjiang.projectschedule.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author jx
 * @description 针对表【user_info】的数据库操作Service实现
 * @createDate 2024-03-20 10:05:43
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
        implements UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private RoleService roleService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ManageDeptMapper manageDeptMapper;

    @Autowired
    private UserService userService;


    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public Result getUserInfo(String token) {
        Integer userId = jwtUtil.getUserId(token);
        UserInfo userInfo = userInfoMapper.selectById(userId);
        if (userInfo == null) {
            return Result.build(null, 201, "获取用户信息失败！", false);
        }
        Integer roleid = userInfo.getRoleid();
        Role role = roleService.getRoleById(roleid);
        userInfo.setRole(role);
        Integer deptid = userInfo.getDeptid();
        if (deptid != null && deptid > 0) {
            Department department = departmentService.getDeptById(deptid);
            userInfo.setDepartment(department);
        }
        if (roleid == 3) {
            LambdaQueryWrapper<ManageDept> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select(ManageDept::getDeptId).eq(ManageDept::getUid, userId);
            List<Object> objects = manageDeptMapper.selectObjs(queryWrapper);
            userInfo.setDepts(objects);
        } else if (roleid == 4 || roleid == 5) {
            List<Object> depts = new ArrayList<>();
            depts.add(deptid);
            userInfo.setDepts(depts);
        }
        return Result.ok(userInfo);
    }

    @Override
    public Result getUserInfoList() {
        List<UserInfo> userInfos = userInfoMapper.selectList(null);
        return Result.ok(userInfos);
    }

    @Override
    public Result updateUserInfo(UserInfo userInfo) {
        UserInfo oriUser = userInfoMapper.selectById(userInfo.getUid());
        LambdaUpdateWrapper<UserInfo> updateWrapper = new LambdaUpdateWrapper<>();
        if (userInfo.getIsAbandon() == 1) userInfo.setAbandonTime(LocalDate.now().toString());
        else userInfo.setAbandonTime(null);
        updateWrapper.eq(UserInfo::getUid, userInfo.getUid())
                .set(UserInfo::getIsAbandon, userInfo.getIsAbandon())
                .set(UserInfo::getAbandonTime, userInfo.getAbandonTime());
        if (userInfo.getDeptid() != oriUser.getDeptid()) {
            updateWrapper.set(UserInfo::getDeptid, userInfo.getDeptid());
        }
        if (userInfo.getRoleid() != oriUser.getRoleid()) {
            updateWrapper.set(UserInfo::getRoleid, userInfo.getRoleid());
        }
        // 更新是否接收短信
        if (userInfo.getSms() != oriUser.getSms()) {
            updateWrapper.set(UserInfo::getSms, userInfo.getSms());
        }

        int rows = userInfoMapper.update(null, updateWrapper);
        if (rows == 1) return Result.ok(null);
        return Result.error(null);
    }

    @Override
    public Result getUserInfoListByRole(Integer roleId) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getRoleid, roleId);
        List<UserInfo> userInfos = userInfoMapper.selectList(queryWrapper);
        return Result.ok(userInfos);
    }

    @Override
    public String getUserNameById(Integer uid) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(UserInfo::getUsername).eq(UserInfo::getUid, uid);
        List<Object> userNames = userInfoMapper.selectObjs(queryWrapper);
        if (!userNames.isEmpty()) return (String) userNames.get(0);
        return null;
    }

    @Override
    public Result insertUserInfo(UserInfo userInfo) {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getIstaffid, userInfo.getIstaffid());
        UserInfo user = userInfoMapper.selectOne(wrapper);
        if (user != null) {
            return Result.build(null, 300, "已存在该用户", false);
        }
        int rows = userInfoMapper.insert(userInfo);
        Long istaffid = userInfo.getIstaffid();
        if (rows == 1) {
            LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select(UserInfo::getUid, UserInfo::getUsername).eq(UserInfo::getIstaffid, istaffid);
            List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
            if (!maps.isEmpty()) {
                Map<String, Object> uInfo = maps.get(0);
                Integer res = userService.insertUser((Integer) uInfo.get("uid"), istaffid, (String) uInfo.get("username"));
                if (res == 1) return Result.ok(null);
            }
        }
        return Result.error(null);
    }

    @Override
    public List<Map<String, Object>> getUserForSendMessage(Integer deptId) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(UserInfo::getUsername)
                .eq(UserInfo::getDeptid, deptId)
                .in(UserInfo::getRoleid, Arrays.asList(4, 5))
                .eq(UserInfo::getSms,1);
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        return maps;
    }
}




